package com.manlitech.cloudboot.baseboot.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.SentinelRpcException;
import com.manlitech.cloudboot.interfacestore.dubbo.DubboServiceI;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * dubbo 测试
 * @author shensg
 */
@Slf4j
@RestController
//@GlobalTransactional(rollbackFor = Exception.class)
public class TestDubboController {

    @DubboReference(check = false)
    private DubboServiceI dubboServiceI;

    /**
     * 测试dubbo请求
     * @param name
     * @return
     */
    @RequestMapping("/dubbo/send")
    @SentinelResource(value = "dubbo" , blockHandler = "exceptionHandler" , fallback = "fallbackHandler")
    public String test(@RequestParam(name = "name", defaultValue = "no name")String name) throws SentinelRpcException {
        RpcContext.getContext().setAttachment("index", "1");
        return dubboServiceI.helloDubbo(name);
    }

    // 限流与阻塞处理
    public void exceptionHandler(String str, BlockException ex) {
        log.error("blockHandler：" + str, ex);
    }

    // 熔断与降级处理
    public void fallbackHandler(String str) {
        log.error("fallbackHandler：" + str);
    }
}
